﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class RotationPlatform6Group : MonoBehaviour {

    public float duration = 0.3f;
    public float rotation = 180f;

    public List<Transform> platforms;

    bool playing;
    WaitForSeconds waitForPlayingEnd;

    private void Start() {
        waitForPlayingEnd = new WaitForSeconds(duration);
    }

    private void OnTriggerEnter2D(Collider2D collision) {
        CharacterData.OnJumpBegin += OnJumpBegin;
    }

    private void OnTriggerExit2D(Collider2D collision) {
        CharacterData.OnJumpBegin -= OnJumpBegin;
    }

    void OnJumpBegin() {
        if (playing)
            return;
        playing = true;
        platforms.ForEach((Transform trans) => {
            OnRotation(trans, rotation, duration);
        });

        StartCoroutine(WaitForPlayingEnd());
    }

    IEnumerator WaitForPlayingEnd() {
        yield return waitForPlayingEnd; 

            playing = false;
    }

    void OnRotation(Transform transform, float rotation, float duration) {
        transform.DOKill(true);
        transform.DOLocalRotateQuaternion(Quaternion.Euler(0, 0, rotation) * transform.localRotation, duration)
                 .PlayForward();
    }

}
